package exercise;

import com.sun.org.slf4j.internal.Logger;
import com.sun.org.slf4j.internal.LoggerFactory;
import org.junit.Test;
import sun.security.util.Length;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: wuhao
 * @since: 2021/5/2 10:41
 */
public class IoExer {

    /**
     * 1.分别使用节点流：FileInputStream、FileOutputStream和缓冲流：
     * BufferedInputStream、BufferedOutputStream实现文本文件/图片/视频文件的
     * 复制。并比较二者在数据复制方面的效率
     * 前提:使用文件一样
     *               FileInputStream、FileOutputStream      BufferedInputStream、BufferedOutputStream
     *  复制文本文件所需耗时:          33ms                                   1ms
     *  复制图片所需耗时:             153ms                                  2ms
     *  复制视频文件所需耗时:          41278ms                               259ms
     */
    //FileInputStream、FileOutputStream 复制文件代码:
    @Test
    public void test1() {
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            fileInputStream = new FileInputStream("D:\\test1\\作文1.txt");
            fileOutputStream = new FileOutputStream("D:\\test2\\作文2.txt");
            //byte [] data = new byte[1024];
            int data;
            long startTime = System.currentTimeMillis();
            //read()读的是int类型字节值
            while ((data = fileInputStream.read()) != -1) {
                fileOutputStream.write(data);
            }
            long endTime = System.currentTimeMillis();
            System.out.println("共花费时间:" + (endTime - startTime) + "ms");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    //BufferedInputStream、BufferedOutputStream复制文件代码:
    @Test
    public void test2() {
        BufferedInputStream bufferedInputStream = null;
        BufferedOutputStream bufferedOutputStream = null;
        try {
            bufferedInputStream = new BufferedInputStream(new FileInputStream("D:\\test1\\春天在哪里1.mp4"));
            bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("D:\\test2\\春天在哪里2.mp4"));
            long startTime = System.currentTimeMillis();
            int data = 0;
            while ((data = bufferedInputStream.read())!=-1){
                    bufferedOutputStream.write(data);
            }
            //刷新缓存区
            bufferedOutputStream.flush();
            long endTime = System.currentTimeMillis();
            System.out.println("共花费时间:" + (endTime - startTime) + "ms");
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                bufferedInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                bufferedOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    /**
     * 2.获取文本上每个字符出现的次数
     * 提示：遍历文本的每一个字符；字符及出现的次数保存在Map中；将Map中数据
     * 写入文件
     */
    @Test
    public void test3(){
        FileReader fileReader = null;
        FileWriter fileWriter = null;
        try {
            //从文本中获取字符出现次数--->创建字符输入流
             fileReader = new FileReader("D:\\test1\\作文1.txt");
             //将Map中数据写入文件--->创建字符输出流
             fileWriter = new FileWriter("D:\\test2\\作文1.txt");
             //从字符流中读
            char [] data = new char[10];
            int length = 0;
            //字符及出现的次数保存在Map中
            Map<Character,Integer> map = new HashMap<>();
            while ((length = fileReader.read(data))!=-1){
                  for (int i = 0;i < length ; i++){
                      char key = data[i];
                      if (map.containsKey(key)){
                          map.put(key, (map.get(key).intValue()+1));
                      }else{
                          map.put(key,1);
                      }
                  }
            }
            fileWriter.write(map.toString());
            fileWriter.flush();
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            try {
                fileReader.close();
            }catch (IOException e){
                e.printStackTrace();
            }
            try {
                fileWriter.close();
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    }
}
